package com.mig.coins.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.mig.coins.db.Consultas;

public class Socio extends Consultas{
	private long id = 0;
	private String nombre = null;
	private String email = null;
	private String login = null;
	private String password = null;
	
	private final static String GET_SOCIO_SQL = "SELECT `socios`.`id`, `socios`.`nombre`, `socios`.`email`, " +
	"`socios`.`login`, `socios`.`password` FROM `socios` ";

	public Socio clone() {
		Socio newSocio = new Socio();
		newSocio.setId(this.id);
		newSocio.setNombre(this.nombre);
		newSocio.setEmail(this.email);
		newSocio.setLogin(this.login);
		newSocio.setPassword(this.password);
		
		return newSocio;
	}
	
	public Long insert(Connection conn) {
		// Validaciones para insertar
		// Insertar
		// Obtiene el id y lo guarda
		return null;
	}

	public boolean delete(Connection conn) {

		return false;
	}

	public boolean update(Connection conn) {
		return false;
	}

	public static Socio readSocio(Connection conn, Long _id) throws SQLException {
		Socio _socio = new Socio(); 
		PreparedStatement stmt=null;
		ResultSet rs = null;

		stmt=conn.prepareStatement(GET_SOCIO_SQL + "WHERE id=?");
		stmt.setLong(1, _id.longValue());
		rs = stmt.executeQuery();
		if (rs.next()) 
			_socio.loadSocioFromResultSet(rs);
		else 
			_socio = null;

		if (rs != null) rs.close();
		if (stmt != null) stmt.close(); 

		return _socio;
	}

	public static ArrayList<Socio> searchSocios(Connection conn, String where, String sort) throws SQLException {
		String query = GET_SOCIO_SQL;
		if ((where != null) && (where.trim().length()>0))
			query +="WHERE " + where;

		if ((sort != null) && (sort.trim().length()>0)) {
			query +="ORDER BY " + sort + " ";
		}
		// Creamos el Statement y el ResultSet.
		Statement stm = conn.createStatement();
		ResultSet rs = stm.executeQuery(query);

		// Procesamos el ResultSet.
		ArrayList<Socio> lSocios = null;
		while (rs.next()) {
			Socio _divisa = new Socio();
			_divisa.loadSocioFromResultSet(rs);
			if (null == lSocios)
				lSocios = new ArrayList<Socio>();
			lSocios.add(_divisa);
		}
		rs.close();
		stm.close();
		return lSocios;
	}

	private void loadSocioFromResultSet(ResultSet rs) throws SQLException {
		this.id = rs.getLong(1);
		this.nombre = rs.getString(2);
		this.email = rs.getString(3);
		this.login = rs.getString(4);
		this.password = rs.getString(5);
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
